<?php
function timeMeasure(){list($msec, $sec) = explode(chr(32), microtime()); return ($sec+$msec);}
define('TIMESTART', timeMeasure());//Определяем константу в которой будем хранить время старта
/*---------------------------------------*/
//if(preg_match('/^.*(aa|bb)(?!.*(cc|kk)).*$/','ggaagghhhhk')){
/*
if(preg_match('/./','')){
	echo 'a';
}else{
	echo 'b';
};
die();
*/

require_once 'include/basic.inc';
require_once 'include/db_credentials.inc';
require_once 'include/config.php';
require_once 'include/DbSimple/Generic.php';
//error_reporting (E_ALL);
//echo 'Расчет<br>';
$oPrj1 = array();
$mydbh = DbSimple_Generic::connect('mysql://stat:stat@localhost/call_stat_in');
$mydbh->query('SET NAMES utf8');
$mydbh->setErrorHandler('databaseErrorHandler');
$rows = $mydbh->select('SELECT * FROM projects');
foreach ($rows as $numRow => $row) {
    //echo $row['id'].$row['name'].'<br>';
    $pr_id = $row['id'];
    $tels = $mydbh->select('SELECT * FROM telephones WHERE pr_id=?d', $pr_id);
    $mytels = array();
    foreach ($tels as $nummtel => $tel) {
    	$mytels[$tel['id']] = array('NUMBER'=>$tel['tel'], 'date1'=>$tel['date1'], 'date2'=>$tel['date2']);
    }
    $oPrj1[$row['id']] = array('NAME'=>$row['name'], 'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>$mytels);
    //print_r($oPrj1);
    //echo '<br>';
}
//print_r($oPrj1);
    //echo '<br>';
$oPrj = array(
'prj_skon'=>				array('NAME'=>'СКОН',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700041/'),
//'prj_umax'=>				array('NAME'=>'Юмакс',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2706024/'),
//'prj_rusich'=>			array('NAME'=>'Русич',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700621/'),
//'prj_sunrise'=>				array('NAME'=>'Санрайз',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700623/'),
//'prj_insys'=>				array('NAME'=>'Инсис',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2789685/'),
//'prj_tri'=>					array('NAME'=>'ТРИ-ТВ',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2700695|2706069)/'),
//'prj_tri1'=>				array('NAME'=>'ТРИ-ТВ 2700695',	'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2700695)/'),
//'prj_tri2'=>				array('NAME'=>'ТРИ-ТВ 2706069',	'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2706069)/'),
'prj_41kanal'=>				array('NAME'=>'41 канал',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700687/'),
	
'prj_dns_ekb'=>				array('NAME'=>'ДНС Екб',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2870506/'),
'prj_dns_perm'=>			array('NAME'=>'ДНС Пермь',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2017595/'),
'prj_dns_chelab'=>			array('NAME'=>'ДНС Челяб',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2250252/'),
'prj_dns_ufa'=>				array('NAME'=>'ДНС Уфа',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/463418/'),
'prj_dns_samara'=>				array('NAME'=>'ДНС Самара',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/8462038130/'),
'prj_dns_nnov'=>				array('NAME'=>'ДНС Н.Нов',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/8312180303/'),
'prj_dns_volg'=>				array('NAME'=>'ДНС Волг',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/8442590057/'),	
'prj_dns_kazan'=>				array('NAME'=>'ДНС Казань',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2118555/'),	
'prj_dns_rostov'=>				array('NAME'=>'ДНС Ростов',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/8632079077/'),
'prj_dns_krasnodar'=>				array('NAME'=>'ДНС Краснодар',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2030124/'),		
//общий DNS
'prj_dns'=>				array('NAME'=>'ДНС',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2870506|2017595|2250252|463418|8462038130|8312180303|8442590057|2118555|8632079077|2030124)/'),		
	
'prj_restoran'=>			array('NAME'=>'Ресторан',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2706045|2372020)/'),
'prj_restoran_ekb'=>		array('NAME'=>'Ресторан Чел', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/73512462020/'),	
'prj_kl-a_usi_beeline'=>	array('NAME'=>'Кл-ая, УСИ, Билайн',	'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2700657|09505642085|7349676|9505645070)/'),
'prj_niagara'=>	array('NAME'=>'Ниагара',	'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/3512477924/'),
'prj_notinuse'=>			array('NAME'=>'(Не используются)',	'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2706084|2706087|2708950)/'),
'prj_citycall1'=>			array('NAME'=>'СС Екб',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2870270/'),
'prj_citycall1_hr'=>				array('NAME'=>'CC Екб HR',	'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2706041/'),
	'prj_citycall2'=>			array('NAME'=>'CC Пермь',		'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/3422017570/'),
	'prj_citycall3'=>			array('NAME'=>'СС Чб',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(3517718270|2477990)/'),
	'prj_citycall4'=>			array('NAME'=>'СС Мск',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/4959843495/'),
	'prj_citycall5'=>			array('NAME'=>'СС Птр',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/8123809280/'),
//	'prj_citycall6'=>			array('NAME'=>'СС Компас',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700695/'),
	'prj_citycall7'=>			array('NAME'=>'СС Новосиб',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/3039943/'),
	'prj_citycall8'=>			array('NAME'=>'СС НН',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/8312180287/'),
	
'prj_kluch'=>				array('NAME'=>'Ключевая',			'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2706085/'),		
//'prj_ttk1'=>					array('NAME'=>'ТТК стар.',				'ANUMBER'=>'/(3727273|3804280)/','ANUMBERDIALED'=>'/2700031/'),	
'prj_ttk'=>					array('NAME'=>'ТТК',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/(2706042|2706043)/'),		
//'prj_titanik'=>				array('NAME'=>'Титаник', 			'ANUMBER'=>'/^(?!.*(3727273|3804280)).*$/', 'ANUMBERDIALED'=>'/2700031/')
'prj_titanik'=>				array('NAME'=>'Титаник', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/2700031/'),
'prj_suet'=>				array('NAME'=>'СУЭТ', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/(2700659|9224490|9224450|9224446)/'),	
//'prj_kassir'=>				array('NAME'=>'Кассир', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/(2706099|2705050)/'),	
'prj_tsg'=>				array('NAME'=>'ТСЖ', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/2723017/'),	
'prj_elita'=>				array('NAME'=>'ЭлитаТрэвел', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/2723019/'),

//'prj_yarmarka'=>				array('NAME'=>'Ярмарка', 			'ANUMBER'=>'/.*/', 'ANUMBERDIALED'=>'/2700645/'),

'prj_ultan'=>array('NAME'=>'Ултан','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700664/'),
'prj_uniontrucks'=>array('NAME'=>'Юнион Тракс','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700656|2049480/'),
'prj_osoo'=>array('NAME'=>'ОСОО','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700646/'),	
'prj_gta'=>array('NAME'=>'ГТА','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700662/'),
//'prj_terma'=>array('NAME'=>'Терма','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/3422017664/'),	
'prj_dragres'=>array('NAME'=>'ДрагРес','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/5555366672/'),
'prj_niagara2'=>array('NAME'=>'Ниагара ГЛ','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2477167/'),
'prj_ndom'=>array('NAME'=>'Наш дом','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2723018/'),
'prj_barstvo'=>array('NAME'=>'Барство','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/3820939/'),
'prj_santeh'=>array('NAME'=>'СантехУрал','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2477092/'),
'prj_decor'=>array('NAME'=>'Декор Блюз','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/3422017560/'),
'prj_foxlab'=>array('NAME'=>'Фокс Лаборатори','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700645/'),
'prj_onix'=>array('NAME'=>'Оникс','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/4999951834/'),
'prj_zdrav'=>array('NAME'=>'Здравница','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2706086/'),
'prj_panorama'=>array('NAME'=>'Панорама','ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2700621/'),
//'prj_'=>array('NAME'=>'','ANUMBER'=>'/.*/','ANUMBERDIALED'=>''),
);

//$oPrj = array(
//	'prj_umax'=>				array('NAME'=>'Юмакс',				'ANUMBER'=>'/.*/','ANUMBERDIALED'=>'/2706024/')
//);


$QUALITY = $_POST['opt']['quality_sec']/86400;	

$dbFrom = $_POST['db']['queue_from'];
$dbTo   = $_POST['db']['queue_to'];

$t1=$dbFrom;
$t2=$dbTo;
dt_parse($t1);
dt_parse($t2);$t2=(int)$t2+1;
$dbWhere = "DATETIME>='{$t1}' AND DATETIME<'{$t2}'";
$dbQuery = "SELECT * FROM I_STATISTICS_CALL_IN WHERE {$dbWhere} ORDER BY DATETIME";

unset($t1,$t2);
//DB OPERATIONS
$STAT = array();
$STATFOOT = array();
$STATDAY = array();
$STATDAYFOOT = array();
$dbHandler = @ibase_connect($db_host, $db_user, $db_password);
$dbResult = ibase_query($dbHandler, $dbQuery);
//$count=0;
//echo '<table>';
while($aRow = ibase_fetch_assoc($dbResult)){
	//++$count;
//	echo '<tr>';
//	parserow($aRow,$s);
//	echo '</tr>';
	//print_r($aRow);
	//echo '<br>';
	parserow($aRow,$STATDAY[$aRow['DATETIME_D']]);
	parserow($aRow,$STATDAYFOOT);
	foreach($oPrj1 as $pr_id=>$prInfo){
		$sName = $pr_id;
		foreach($prInfo['ANUMBERDIALED'] as $telid=>$tel)
		{
			$date1_s = $tel['date1'];
			$date2_s = $tel['date2'];
			$rowDate = daysToDateTime($aRow['DATETIME_D']);
			$date1 = date_create($date1_s);
			$date2 = date_create($date2_s);
			$isourday = (($rowDate>=$date1) and ($rowDate<=$date2));
			$pattern = '/'.trim($tel['NUMBER']).'/';
			// if ($prInfo['NAME']=='СКОН')
			// {
			// echo $pattern.' ';
			// echo $date1->format('Y-m-d').' ';
			// echo $date2->format('Y-m-d').' ';
			// echo $rowDate->format('Y-m-d').' ';
			// echo $aRow['ANUMBERDIALED'].' ';
			// if ($isourday)
			// echo 'true';
			// else
			// echo 'false ';
			// echo preg_match('/2700041/',$aRow['ANUMBERDIALED']).' ';
			// echo '<br>';
			// }
			if( (preg_match($pattern,$aRow['ANUMBERDIALED'])) && $isourday){
				//if(!isset($STAT[$sName]=array()));
				parserow(&$aRow,&$STAT[$aRow['DATETIME_D']][$sName]);
				parserow(&$aRow,&$STATFOOT[$sName]);
				//break;
			}
			// if( (preg_match($prInfo['ANUMBER'],$aRow['ANUMBER'])) && (preg_match($pattern,$aRow['ANUMBERDIALED'])) && $isourday){
				// parserow(&$aRow,&$STAT[$aRow['DATETIME_D']][$sName]);
				// parserow(&$aRow,&$STATFOOT[$sName]);
			// }
		}
	}
}
	
//echo '</table>';
ibase_free_result ($dbResult);
ibase_close ($dbHandler);

//echo 'Нет результатов';
//!!!!!
//die();

if( !(count($STAT) > 0) )die();

require_once 'include/config.php';
require_once 'include/DbSimple/Generic.php';
$dbh = DbSimple_Generic::connect('mysql://stat:stat@localhost/infinity_stat');
$dbh->query('SET NAMES utf8');
$dbh->setErrorHandler('databaseErrorHandler');

function databaseErrorHandler($message, $info){
	if (!error_reporting()) return;
	echo "SQL Error: $message<br><pre>"; 
	print_r($info);
	echo "</pre>";
	exit();
}
//print_r($STAT);
$site_dt = $dbh->query('SELECT MAX(`date`) as `max_date` FROM `stat_site`');
if( isset($site_dt[0]['max_date']) ) $d2 = new DateTime($site_dt[0]['max_date']);

//save to table stat_in
$mydbh->query('DELETE FROM `stat_in` WHERE 1');
$init = array();
foreach($STAT as $day=>$prjs){
	$d = d_converter($day);
	$init['datetime'] = $d;
	foreach($prjs as $name=>$arr){
		$init['pr_id'] = $name;
		$init['call'] = $arr['count_all'] ? $arr['count_all']:0;
		$init['min'] = $arr['count_opers']|0;
		$init['waste'] = ($arr['count_agentsCell'] + $arr['count_agents']) | 0;
		$init['service'] = $arr['count_lost']|0;
		//print_r($init);
		$res123 = $mydbh->query('INSERT IGNORE INTO `stat_in`(?#) VALUES(?a)' ,array_keys($init),array_values($init));
		//print_r($res123);
		//echo '<br>';
	}
	
}
//end save

//tbl head
//if(isset($_POST['opt']['minup'])){
echo '<div id="minup" style="display:none">';
echo '<table class="tbl_stat" border="1">';
echo '<thead>';
echo '<tr>';
	echo '<th>День</th>';
	echo '<th colspan=4 style="border-right: solid 3px">Общее</th>';
	$fk = key($STAT);
	foreach($oPrj1 as $name=>$array){
		echo '<th colspan=4 style="border-right: solid 3px">',$array['NAME'],'</th>';
	}
echo '</tr>';
echo '</thead><tbody>';
echo '<tr style="background:#E8E7C5;font-size:small">';
	echo '<td></td>';
	$jc = count($oPrj1);
	for($j=0 ; $j < $jc+1 ; $j++){
		echo '<td class="vertical">Кол-во звонков</td>';
		echo '<td class="vertical">Кол-во минут</td>';
		echo '<td class="vertical">% потерь</td>';
		echo '<td class="vertical" style="border-right: solid 3px">уровень обсл,%</td>';
	}
echo '</tr>';

//eof tbl head
foreach($STAT as $day=>$prj){
	
	echo '<tr>';
	$wday = d_conv($day);
	if($wday[0]==0 || $wday[0]==6){$col="#8CE4FF";}else{$col="#C5E2E8";}
	echo "<td style=\"background:{$col}\" class=\"first_row\">",$wday[1],'</td>';
	
	//echo '</tr><tr>';
	if(isset($STATDAY[$day])){
		$val=$STATDAY[$day];
		echo '<td>',$val['count_all'],'</td>';
		echo '<td>',($val['dur_opersUp']),'</td>';
		echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'';echo '</td>';
		isset($val['withoutIVR'])?$q = round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):$q='';
			if($q<70){
				$col='#D69696';
			}else if($q<80){
				$col='#DDE3A1';
			}else{
				$col='#9DD696';
			}
		echo "<td style=\"background:{$col};border-right: solid 3px\">",$q,'</td>';
	}else{
		echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
	foreach($oPrj1 as $k=>$valnotneeded){
		if(isset($prj[$k])){
			$val=$prj[$k];
			echo '<td>',$val['count_all'],'</td>';
			echo '<td>',($val['dur_opersUp']),'</td>';
			echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'';echo '</td>';
			isset($val['withoutIVR'])?$q = round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):$q='';
				if($q<70){
					$col='#D69696';
				}else if($q<80){
					$col='#DDE3A1';
				}else{
					$col='#9DD696';
				}
			echo "<td style=\"background:{$col};border-right: solid 3px\">",$q,'</td>';
		}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
		}
	}
	
	echo '</tr>';
}
//mean
$c = count($STAT);
echo '</tr><tr class="stat_mean"><td>Среднее</td>';
	if(isset($STATDAYFOOT)){
		$val=$STATDAYFOOT;
		echo '<td>',round($val['count_all']/$c),'</td>';
		echo '<td>',round(($val['dur_opersUp'])/$c),'</td>';
		echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'','</td>';
		echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
		echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
foreach($oPrj1 as $k=>$valnotneeded){
	if(isset($STATFOOT[$k])){
			$val=$STATFOOT[$k];
	echo '<td>',round($val['count_all']/$c),'</td>';
	echo '<td>',round(($val['dur_opersUp'])/$c),'</td>';
	echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'','</td>';
	echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
}
//eof mean

//total
echo '</tr><tr class="stat_total"><td>Итого</td>';
	if(isset($STATDAYFOOT)){
				$val=$STATDAYFOOT;
		echo '<td>',$val['count_all'],'</td>';
		echo '<td>',($val['dur_opersUp']),'</td>';
		echo '<td></td>';//echo '<td>',round($val['count_lost']/$val['count_all']*100),'</td>';
		echo '<td style="border-right: solid 3px"></td>';//echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
				echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
foreach($oPrj1 as $k=>$valnotneeded){
	if(isset($STATFOOT[$k])){
			$val=$STATFOOT[$k];
	echo '<td>',$val['count_all'],'</td>';
	echo '<td>',($val['dur_opersUp']),'</td>';
	echo '<td></td>';//echo '<td>',round($val['count_lost']/$val['count_all']*100),'</td>';
	echo '<td style="border-right: solid 3px"></td>';//echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
}
//eof total
echo '</tr></tbody></table>';

echo '</div>';
//}else{////////////////////////////////////////////////////
echo '<div id="plain">';

echo '<table class="tbl_stat" border="1">';
echo '<thead>';
echo '<tr>';
	echo '<th>День</th>';
	echo '<th colspan=4 style="border-right: solid 3px">Общее</th>';
	$fk = key($STAT);
	foreach($oPrj1 as $name=>$array){
		echo '<th colspan=4 style="border-right: solid 3px">',$array['NAME'],'</th>';
	}
echo '</tr>';
echo '</thead><tbody>';
echo '<tr style="background:#E8E7C5;font-size:small">';
	echo '<td></td>';
	$jc = count($oPrj1);
	for($j=0 ; $j < $jc+1 ; $j++){
		echo '<td class="vertical">Кол-во звонков</td>';
		echo '<td class="vertical">Кол-во минут</td>';
		echo '<td class="vertical">% потерь</td>';
		echo '<td class="vertical" style="border-right: solid 3px">уровень обсл,%</td>';
	}
echo '</tr>';

//eof tbl head
foreach($STAT as $day=>$prj){
	
	echo '<tr>';
	$wday = d_conv($day);
	if($wday[0]==0 || $wday[0]==6){$col="#8CE4FF";}else{$col="#C5E2E8";}
	echo "<td style=\"background:{$col}\" class=\"first_row\">",$wday[1],'</td>';
	
	//echo '</tr><tr>';
	if(isset($STATDAY[$day])){
		$val=$STATDAY[$day];
		echo '<td>',$val['count_all'],'</td>';
		echo '<td>',t_conv($val['dur_opers']),'</td>';
		echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'';echo '</td>';
		isset($val['withoutIVR'])?$q = round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):$q='';
			if($q<70){
				$col='#D69696';
			}else if($q<80){
				$col='#DDE3A1';
			}else{
				$col='#9DD696';
			}
		echo "<td style=\"background:{$col};border-right: solid 3px\">",$q,'</td>';
	}else{
		echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
	foreach($oPrj1 as $k=>$valnotneeded){
		if(isset($prj[$k])){
			$val=$prj[$k];
			echo '<td>',$val['count_all'],'</td>';
			echo '<td>',t_conv($val['dur_opers']),'</td>';
			echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'';echo '</td>';
			isset($val['withoutIVR'])?$q = round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):$q='';
				if($q<70){
					$col='#D69696';
				}else if($q<80){
					$col='#DDE3A1';
				}else{
					$col='#9DD696';
				}
			echo "<td style=\"background:{$col};border-right: solid 3px\">",$q,'</td>';
		}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
		}
	}
	echo '</tr>';
}
//mean
$c = count($STAT);
echo '</tr><tr class="stat_mean"><td>Среднее</td>';
	if(isset($STATDAYFOOT)){
		$val=$STATDAYFOOT;
		echo '<td>',round($val['count_all']/$c),'</td>';
		echo '<td>',round(t_conv($val['dur_opers'])/$c),'</td>';
		echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'','</td>';
		echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
		echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
foreach($oPrj1 as $k=>$valnotneeded){
	if(isset($STATFOOT[$k])){
			$val=$STATFOOT[$k];
	echo '<td>',round($val['count_all']/$c),'</td>';
	echo '<td>',round(t_conv($val['dur_opers'])/$c),'</td>';
	echo '<td>',isset($val['count_all'])?round($val['count_lost']/$val['count_all']*100):'','</td>';
	echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
}
//eof mean

//total
echo '</tr><tr class="stat_total"><td>Итого</td>';
	if(isset($STATDAYFOOT)){
				$val=$STATDAYFOOT;
		echo '<td>',$val['count_all'],'</td>';
		echo '<td>',t_conv($val['dur_opers']),'</td>';
		echo '<td></td>';//echo '<td>',round($val['count_lost']/$val['count_all']*100),'</td>';
		echo '<td style="border-right: solid 3px"></td>';//echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
				echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
foreach($oPrj1 as $k=>$valnotneeded){
	if(isset($STATFOOT[$k])){
			$val=$STATFOOT[$k];
	echo '<td>',$val['count_all'],'</td>';
	echo '<td>',t_conv($val['dur_opers']),'</td>';
	echo '<td></td>';//echo '<td>',round($val['count_lost']/$val['count_all']*100),'</td>';
	echo '<td style="border-right: solid 3px"></td>';//echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
}

echo '</tr><tr class="stat_total"><td>Итого</td>';
echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
foreach($oPrj1 as $k=>$valnotneeded){
	if(isset($STATFOOT[$k])){
			$val=$STATFOOT[$k];
	echo '<td>г:',$val['count_agents'],'</td>';
	echo '<td> ',$val['dur_ext'],'</td>';
	echo '<td>с: ',$val['count_agentsCell'],'</td>';//echo '<td>',round($val['count_lost']/$val['count_all']*100),'</td>';
	echo '<td style="border-right: solid 3px">',$val['dur_extCell'],'</td>';//echo '<td style="border-right: solid 3px">',isset($val['withoutIVR'])?round(($val['withoutIVR']-$val['bad'])/$val['withoutIVR']*100):'';echo '</td>';
	}else{
			echo '<td></td><td></td><td></td><td style="border-right: solid 3px"></td>';
	}
}

//eof total
echo '</tr></tbody></table>';

//}
echo '</div>';

echo '<br/>Страница сгенерировалась за ',round(timeMeasure()-TIMESTART, 6),' сек.';
?>